﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Email.Subscription;

public partial class Member_Subscription_SubscriptionProductEdit : nspBase.MemberBasePge
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (CheckCookies())
        {
            if (!IsPostBack)
            {
                Bind();
            }
        }
        else
            Response.Redirect("../../login/login.aspx");
    }
    private void Bind()
    {
        if (Request.QueryString["ID"] != null && Request.QueryString["ID"] != "")
        {
            int id = int.Parse(Request.QueryString["ID"].ToString());
            SubscriptionProduct product = new SubscriptionProduct();
            SubscriptionProduct prod = product.GetItem(id);
            if (prod.Type != "")
            {
                for (int i = 0; i < chklProduct.Items.Count; i++)
                {
                    if (prod.Type == chklProduct.Items[i].Value)
                        chklProduct.Items[i].Selected = true;

                }
            }
            txtKeyword.Text = prod.Keyword;
            if (prod.Area != "")
            {
                string[] strArea = prod.Area.Split(new char[] { ',' });
                txtProvince.Value = strArea[0];
                txtCity.Value = strArea[1];
                txtCounty.Value = strArea[2];
            }
            if (prod.Indtype != null)
            {
                string[] str = prod.Indtype.Split(new char[] { ',' });
                for (int i = 0; i < str.Length; i++)
                {
                    if (i == 0)
                        this.txtSortB.Value = str[i];
                    else if (i == 1)
                        this.txtSortM.Value = str[i];
                    else
                        this.txtSortS.Value = str[i];
                }
            }
            dropValidity.SelectedValue = prod.Validity.ToString();
        }
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (Request.QueryString["ID"] != null && Request.QueryString["ID"] != "")
        {
            try
            {
                int id = int.Parse(Request.QueryString["ID"].ToString());
                SubscriptionProduct product = new SubscriptionProduct();
                product.ID = id;
                string myValue = "";
                myValue += Request.Form["ctl00$ContInfo$SortB"];
                myValue += ',' + Request.Form["ctl00$ContInfo$SortM"];
                myValue += ',' + Request.Form["ctl00$ContInfo$SortS"];
                product.Indtype = myValue;
                myValue = "";
                product.Type = chklProduct.SelectedValue;
                product.Keyword = txtKeyword.Text.Trim();
                myValue += Request.Form["ctl00$ContInfo$province"] + ',';
                myValue += Request.Form["ctl00$ContInfo$city"] + ',';
                myValue += Request.Form["ctl00$ContInfo$county"];
                product.Area = myValue;
                product.Validity = int.Parse(dropValidity.SelectedValue);
                product.Update(product);
                ShowMessage("商机订阅成功！", "Subscription/SubscriptionProductList.aspx");


            }
            catch (Exception ex)
            {
                ShowMessage(ex.Message);
            }
        }
    }
}
